
//导入
// import instance from '../../utils/request'
import instance from '../../utils/http'
Page({
    //测试嵌套请求，加载框保持一个
    handerl_getqt() {
        var url = "/api/3/stories/latest"
        instance.get(url,
            { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' }
            , { timeout: 2000, header: { 'Content-Type': 'application/json', },isLoading:true }//这个就是config
        ).then((res) => {
            instance.get(url,
                { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' }
                , { timeout: 2000, header: { 'Content-Type': 'application/json' } }//这个就是config
            )
        })
    }
    ,
    //并发请求
    async handerl_getAll() {
        var url = "/api/3/stories/latest"
        try {
            //Promise.all并发请求,res是数组
            const res = await instance.all(
                [instance.get(url,
                    { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' }
                    , { timeout: 2000, header: { 'Content-Type': 'application/json' } }//这个就是config
                ),
                instance.post(url,
                    { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' }
                    , { timeout: 2000, header: { 'Content-Type': 'application/json' } }//这个就是config
                ),
                instance.get(url,
                    { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' }
                    , { timeout: 2000, header: { 'Content-Type': 'application/json' } }//这个就是config
                )])
            console.log("handerl_getAll res=", res);
        } catch (error) {//代码错误回调的是reject
            console.log("handerl_getAll error=", error);
        }
    },
    //简化版第2种方式
    async handerl_get2() {
        //
        var url = "/api/3/stories/latest"
        try {
            //await只接受成功回掉resolve
            const res = await instance.get(url,
                { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' }
                , { timeout: 2000, header: { 'Content-Type': 'application/json' } }//这个就是config
            )
            console.log("handerl_get2 res=", res);
        } catch (error) {//代码错误回调的是reject
            console.log("请求失败 handerl_get2=", error);
        }
    },
    //简化版第一种方式
    handerl_get() {
        var url = "/api/3/stories/latest"
        instance.get(url,
            { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' })
            .then(res => {
                console.log('handler_get res=', res);
            }).catch((error) => {
                console.log("error:", error);
            })
    },
    //复杂使用第一种：使用“.then”方式
    handler_then() {
        instance.request({
            // url:"https://news-at.zhihu.com/api/3/stories/latest",
            url: "/api/3/stories/latest",
            method: "get",
            data: { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' },
            header: {
                'Content-Type': 'application/json'
            },
        }).then(res => {
            console.log('handler_then res=', res);
        }).catch(error => {
            console.log("失败=", error);
        })
    },
    //第二种使用async，await方式
    async handler_await_async() {
        const res = await instance.request({
            url: "https://news-at.zhihu.com/api/3/stories/latest",
            method: "get",
            data: { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' },
        })
        console.log("handler_await_async res=", res);
    },
    loginClick(event) {
        wx.request({
            //   url:"https://gmall-prod.atguigu.cn/mall-pai/index/findBanner",
            url: "https://news-at.zhihu.com/api/3/stories/latest",
            method: 'POST',
            data: { "username": 'BA130108', "password": 'Fo2Dgix2u_Tgupps9W_Fdg~~' },//请求参数
            header: {
                'Content-Type': 'application/json'
            },
            success: (res) => {
                console.log('success=', res)
            },
            fail: (res) => {
                console.log('fail=', res)
            },
            //成功失败都会走
            complete: () => {
                console.log('请求结束')
            }
        })
    },
    data:{
        imgurl:'../../images/sy1.png'
    },
    //文件上传
    caClick(event){
        console.log(event.detail);
        const {avatarUrl}=event.detail
        this.setData({
            imgurl:avatarUrl
        })
        instance.upload("/mall-api/fileUpload",avatarUrl,'file')
        // wx.uploadFile({
        //     url: 'https://gmall-prod.atguigu.cn/mall-api/fileUpload',
        //   filePath: this.data.imgurl,
        //   //服务器根据这个file来获取
        //   name: 'file',
        //  success:(res)=>{
        //     res.data=JSON.parse(res.data)
        //     //获取地址
        //     console.log("res=",res.data.data);
        //  }
        // })
    },

})